#!/usr/bin/env python
# -*- encoding: utf-8 -*-


# error schema
{
    'error': {
        'errortype': int,
        'taskid': str,  # new, not change
        'project': str,  # new, not change
        'date': str,  #
        'url': str,  # new, not change
        'status': int,  # change
        'schedule': {
            'priority': int,
            'retries': int,
            'retried': int,
            'exetime': int,
            'age': int,
            'itag': str,
            # 'recrawl': int
        },  # new and restart
        'fetch': {
            'method': str,
            'headers': dict,
            'data': str,
            'timeout': int,
            'save': dict,
        },  # new and restart
        'process': {
            'callback': str,
        },  # new and restart
        'track': {
            'fetch': {
                'ok': bool,
                'time': int,
                'status_code': int,
                'headers': dict,
                'encoding': str,
                'content': str,
            },
            'process': {
                'ok': bool,
                'time': int,
                'follows': int,
                'outputs': int,
                'logs': str,
                'exception': str,
            },
            'save': object,  # jsonable object saved by processor
        },  # finish
        'lastcrawltime': int,  # keep between request
        'updatetime': int,  # keep between request
        'mark': bool,  # int in sqlite
        'result': str,
        'group': str,
        'project_updatetime': int,
        'project_md5sum': str,
        'vacancy_dates': int
    }
}


class ErrorDB(object):
    TASK_ERROR = 1
    DB_ERROR = 2
    VACANCY_ERROR = 3

    @staticmethod
    def error_type_to_string(status):
        return {
            1: 'TASK_ERROR',
            2: 'DB_ERROR',
            3: 'VACANCY_ERROR'
        }.get(status, 'UNKNOWN')

    def get_tasks(self, error_type, mark=False, limit=1000):
        raise NotImplementedError

    def get_task(self, project, taskid):
        raise NotImplementedError

    def insert(self, project, taskid, obj):
        raise NotImplementedError

    def insert_vacancy(self, error_type, project, date, group):
        raise NotImplementedError

    def update(self, project, taskid, obj):
        raise NotImplementedError

    def count(self, error_type, hour=None):
        raise NotImplementedError

    def count_by_date(self, error_type, date):
        raise NotImplementedError

    def delete(self, project, date):
        raise NotImplementedError

    def select(self, error_type, hour=None, fields=None, offset=0, limit=1000):
        raise NotImplementedError
